package com.gin.no_gay_alliance.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.DrawableRes;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.gin.jackson.utils.ObjectUtils;
import com.gin.no_gay_alliance.activity.UserCenterActivity;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.utils.ImageUtils;
import com.gin.no_gay_alliance.utils.TextViewTarget;

/**
 * 用户链接,包含头像和用户中心跳转
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/1 15:34
 **/
public class UserLinkView extends androidx.appcompat.widget.AppCompatTextView implements View.OnClickListener {
    /**
     * 头像放置的位置:左/上/右/下/;
     */
    @IntRange(from = 0, to = 3)
    int position = 0;
    /**
     * 头像的大小
     */
    int size = 48;
    private Long userId;
    private String username;


    public UserLinkView(@NonNull Context context) {
        super(context);
    }

    public UserLinkView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public UserLinkView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void init(Long userId, String username, String avatar, int size, @IntRange(from = 0, to = 3) int position) {
        this.userId = userId;
        this.username = username;
        this.position = position;

        setText(username);
        setOnClickListener(this);

        if (!ObjectUtils.isEmpty(avatar)) {
            int s = MyApplication.dip2px(size);
            ImageUtils.loadUserAvatar(getContext(), avatar, new TextViewTarget(this, this.position, s, s));
        }
    }

    public void init(Long userId, String username, @DrawableRes int resId, int size, @IntRange(from = 0, to = 3) int position) {
        this.userId = userId;
        this.username = username;
        this.position = position;

        setText(username);
        setOnClickListener(this);
        int s = MyApplication.dip2px(size);
        Glide.with(getContext()).load(resId).into(new TextViewTarget(this, position, s, s));
    }

    @Override
    public void onClick(View v) {
        //跳转用户中心
        if (userId != null) {
            UserCenterActivity.startActivity(getContext(), userId);
        } else if (username != null) {
            UserCenterActivity.startActivity(getContext(), username);
        }
    }
}
